package com.coderg.gmall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coderg.gmall.common.constant.ProductConst;
import com.coderg.gmall.common.result.Result;
import com.coderg.gmall.model.product.BaseAttrInfo;
import com.coderg.gmall.model.product.BaseTrademark;
import com.coderg.gmall.model.product.SkuInfo;
import com.coderg.gmall.model.product.SpuInfo;
import com.coderg.gmall.product.service.ManageService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description 分类和平台属性相关接口
 * @Author CoderG
 * @Date 2025/5/18 22:25
 **/

@Api(tags = "分类和平台属性相关接口")
@RestController
@RequestMapping(value = "/admin/product")
public class ManageController {

    @Autowired
    ManageService manageService;


    @GetMapping("/getCategory1")
    public Result findCategory1() {
        return Result.ok(manageService.findCategory1());
    }


    @GetMapping("/getCategory2/{category1Id}")
    public Result findCategory2(@PathVariable("category1Id") Long id) {
        return Result.ok(manageService.findCategory2(id));
    }

    @GetMapping("/getCategory3/{category2Id}")
    public Result findCategory3(@PathVariable("category2Id") Long id) {
        return Result.ok(manageService.findCategory3(id));
    }

    /**
     * 添加平台属性
     *
     * @param listMap
     * @return
     */
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfoAndValue(@RequestBody Map<String, Object> listMap) {
        manageService.saveAttrInfoAndValue(listMap);
        return Result.ok();
    }

    /**
     * 根据三级分类id获取平台属性
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getAttrInfoAndValue(@PathVariable("category1Id") Long category1Id,
                                      @PathVariable("category2Id") Long category2Id,
                                      @PathVariable("category3Id") Long category3Id) {
        if (category3Id == null) {
            throw new RuntimeException("参数错误");
        }
        List<BaseAttrInfo> list = manageService.getAttrInfoList(category3Id);
        return Result.ok(list);
    }

    /**
     * 根据平台属性ID获取平台属性和值
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据平台属性ID获取平台属性和值")
    @GetMapping("getAttrValueList/{attrId}")
    @ApiParam(name = "id", value = "平台属性ID", required = true)
    @ApiResponses({
            @ApiResponse(code = 200, message = "获取成功", response = String.class),
            @ApiResponse(code = 201, message = "未找信息", response = String.class)})
    public Result getAttrValueListById(@PathVariable("attrId") Long id) {
        BaseAttrInfo baseAttrInfo = manageService.getAttrValueListById(id);
        return Result.ok(baseAttrInfo);
    }

    @ApiOperation("获取spu分页列表")
    @GetMapping(value = "{page}/{limit}")
    public Result getSpuPage(@ApiParam(name = "page", value = "当前页", required = true) @PathVariable("page") Long page,
                             @ApiParam(name = "limit", value = "每页大小", required = true) @PathVariable("limit") Long limit,
                             @ApiParam(name = "category3Id", value = "三级目录id", required = true) Long category3Id) {
        Page spuPage = manageService.getSpuPage(page, limit, category3Id);
        return Result.ok(spuPage);
    }

    @ApiOperation("获取品牌列表")
    @GetMapping(value = "/baseTrademark")
    public Result getTrademarkList() {
        List<BaseTrademark> trademarks = manageService.getTrademarkList();
        return Result.ok(trademarks);
    }

    @ApiOperation("获取销售属性列表")
    @GetMapping(value = "/baseSaleAttrList")
    public Result getBaseSaleAttrList() {
        return Result.ok(manageService.getBaseSaleAttrList());
    }

    // qwertyuiop[{}}[}] asdfghjkl;:''""  zxcvbnm,<.>/???//
    @ApiOperation("保存spuInfo")
    @PostMapping(value = "/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    @ApiOperation("获取销售属性值")
    @GetMapping(value = "/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttr(@PathVariable("spuId") Long spuId) {
        return Result.ok(manageService.getSpuSaleAttr(spuId));
    }

    @ApiOperation("获取spuImage")
    @GetMapping(value = "/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable("spuId") Long spuId) {
        return Result.ok(manageService.getSpuImageListBy(spuId));
    }

    @ApiOperation("保存skuInfo")
    @PostMapping(value = "/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    @ApiOperation("skuInfo分页查询")
    @GetMapping(value = "/list/{page}/{limit}")
    public Result getSkuInfoPage(@PathVariable("page") Long page,
                                 @PathVariable("limit") Long limit) {
        IPage skuInfoPage = manageService.getSkuInfoPage(page, limit);
        return Result.ok(skuInfoPage);
    }
    @ApiOperation("skuInfo上架")
    @GetMapping(value = "/onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId){

        manageService.upOrDown(skuId, ProductConst.SKU_SALE);
        return Result.ok();
    }
    @ApiOperation("skuInfo下架")
    @GetMapping(value = "/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId){

        manageService.upOrDown(skuId,ProductConst.SKU_CANCEL);
        return Result.ok();
    }
}
